import tkinter as tk
import os
from text_editor import TenderTextEditor

class TextEditorGUI:
    def __init__(self, root):
        self.root = root
        self.root.title('投标文件制作软件')
        self.editor = TenderTextEditor()

        # 创建组件
        self.create_widgets()

    def create_widgets(self):
        # 收集文本按钮
        self.collect_btn = tk.Button(self.root, text='收集文本', command=self.collect_text)
        self.collect_btn.pack(pady=10)

        # 分类文本按钮
        self.classify_btn = tk.Button(self.root, text='分类文本', command=self.classify_text)
        self.classify_btn.pack(pady=10)

        # 组织文本按钮
        self.organize_btn = tk.Button(self.root, text='组织文本', command=self.organize_text)
        self.organize_btn.pack(pady=10)

        # 统一格式按钮
        self.format_btn = tk.Button(self.root, text='统一格式', command=self.unify_format)
        self.format_btn.pack(pady=10)

        # 添加注释按钮
        self.annotate_btn = tk.Button(self.root, text='添加注释', command=self.add_annotation)
        self.annotate_btn.pack(pady=10)

        # 批量替换按钮
        self.replace_btn = tk.Button(self.root, text='批量替换', command=self.batch_replace)
        self.replace_btn.pack(pady=10)

        # 提醒按钮
        self.reminder_btn = tk.Button(self.root, text='提醒', command=self.reminder)
        self.reminder_btn.pack(pady=10)

    def collect_text(self):
        from tkinter import filedialog
        file_path = filedialog.askopenfilename()
        if not file_path:
            return
        result = self.editor.collect_text(file_path)
        if result:
            self.current_file_path = file_path
            print('文本收集成功')
        else:
            print('文本收集失败')

    def classify_text(self):
        if not hasattr(self, 'current_file_path'):
            print('请先选择文件')
            return
        file_name = os.path.basename(self.current_file_path)
        category = '项目背景'
        result = self.editor.classify_text(file_name, category)
        if result:
            print('文本分类成功')
        else:
            print('文本分类失败')

    def organize_text(self):
        organized = self.editor.organize_text()
        print('组织后的文本：', organized)

    def unify_format(self):
        organized = self.editor.organize_text()
        formatted = self.editor.unify_format(organized)
        print('格式化后的文本：', formatted)

    def add_annotation(self):
        organized = self.editor.organize_text()
        formatted = self.editor.unify_format(organized)
        annotated = self.editor.add_annotation(formatted, '重要信息', 10)
        print('添加注释后的文本：', annotated)

    def batch_replace(self):
        organized = self.editor.organize_text()
        formatted = self.editor.unify_format(organized)
        annotated = self.editor.add_annotation(formatted, '重要信息', 10)
        replaced = self.editor.batch_replace(annotated, {'项目名称': '新的项目名称'})
        print('替换后的文本：', replaced)

    def reminder(self):
        self.editor.reminder('提交投标文件', '2024-12-31')

if __name__ == '__main__':
    root = tk.Tk()
    app = TextEditorGUI(root)
    root.mainloop()